﻿using Pipes.Data;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Pipes.UnitTest
{
    
    
    /// <summary>
    ///This is a test class for DelimitedTextDataReaderTest and is intended
    ///to contain all DelimitedTextDataReaderTest Unit Tests
    ///</summary>
  [TestClass()]
  public class DelimitedTextDataReaderTest
  {


    private TestContext testContextInstance;

    /// <summary>
    ///Gets or sets the test context which provides
    ///information about and functionality for the current test run.
    ///</summary>
    public TestContext TestContext
    {
      get
      {
        return testContextInstance;
      }
      set
      {
        testContextInstance = value;
      }
    }

    #region Additional test attributes
    // 
    //You can use the following additional attributes as you write your tests:
    //
    //Use ClassInitialize to run code before running the first test in the class
    //[ClassInitialize()]
    //public static void MyClassInitialize(TestContext testContext)
    //{
    //}
    //
    //Use ClassCleanup to run code after all tests in a class have run
    //[ClassCleanup()]
    //public static void MyClassCleanup()
    //{
    //}
    //
    //Use TestInitialize to run code before running each test
    //[TestInitialize()]
    //public void MyTestInitialize()
    //{
    //}
    //
    //Use TestCleanup to run code after each test has run
    //[TestCleanup()]
    //public void MyTestCleanup()
    //{
    //}
    //
    #endregion


    /// <summary>
    ///A test for GetEnumerator
    ///</summary>
    [TestMethod()]
    public void DelimitedDataReader_Headers()
    {
      var input = "col1,col2\nrow1.1,row1.2\nrow2.1,row2.2";
      var expected = new KeyValuePair<string, object>[,] { {new KeyValuePair<string, object>("col1", "row1.1"), new KeyValuePair<string, object>("col2", "row1.2")},
                                                           {new KeyValuePair<string, object>("col1", "row2.1"), new KeyValuePair<string, object>("col2", "row2.2")}};
      var delim = new IO.StringTextSource(input).Pipe(new DelimitedTextLineReader());
      delim.FieldEnclosingChar = '"';
      delim.EnclosingCharEscape = '"';
      delim.AddDelim(',');
      var data = delim.Pipe(new DelimitedTextDataReader());
      data.HasHeaders = true;

      int i = 0, j = 0;
      foreach (var record in data)
      {
        j = 0;
        foreach (var field in record)
        {
          Assert.AreEqual(field.Name, expected[i, j].Key);
          Assert.AreEqual(field.Value, expected[i, j].Value);
          j++;
        }
        i++;
      }
    }

    [TestMethod()]
    public void DelimitedDataReader_NoHeaders()
    {
      var input = "col1,col2\nrow1.1,row1.2\nrow2.1,row2.2";
      var expected = new KeyValuePair<string, object>[,] { {new KeyValuePair<string, object>("0", "col1"), new KeyValuePair<string, object>("1", "col2")},
                                                           {new KeyValuePair<string, object>("0", "row1.1"), new KeyValuePair<string, object>("1", "row1.2")},
                                                           {new KeyValuePair<string, object>("0", "row2.1"), new KeyValuePair<string, object>("1", "row2.2")}};
      var delim = new IO.StringTextSource(input).Pipe(new DelimitedTextLineReader());
      delim.FieldEnclosingChar = '"';
      delim.EnclosingCharEscape = '"';
      delim.AddDelim(',');
      var data = delim.Pipe(new DelimitedTextDataReader());
      data.HasHeaders = false;

      int i = 0, j = 0;
      foreach (var record in data)
      {
        j = 0;
        foreach (var field in record)
        {
          Assert.AreEqual(expected[i, j].Key, field.Name);
          Assert.AreEqual(expected[i, j].Value, field.Value);
          j++;
        }
        i++;
      }
    }
  }
}
